package servlet;

import login.JDBCHelper;
import login.User.User;
import login.User.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();

        User u=new User();
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String realName=request.getParameter("realName");
        String birthDate=request.getParameter("birthDate");
        String email=request.getParameter("email");
        String telephone=request.getParameter("telephone");
        String address=request.getParameter("address");
        String postcode=request.getParameter("postcode");

        String vail=request.getSession().getAttribute("vail").toString();

        //客户端验证码
        String vail1=request.getParameter("vail");

        if(vail==""||vail==null){
            out.write("验证码不能为空，3秒后跳转回注册页面重新注册");
            response.setHeader("Refresh", "3;url=Register.jsp");
        }
        //验证验证码是否相等
        else  if(vail.equals(vail1)) {
            if (name == null || password == null || realName == null || email == null) {
                out.write("必填项不能为空，3秒后跳转回注册页面重新注册");
                response.setHeader("Refresh", "3;url=Register.jsp");
            } else {
                try {
                    if (queryName(name)) {//如果用户名不存在
                        u.setName(name);
                        u.setPassword(password);
                        u.setRealName(realName);
                        u.setBirthDate(birthDate);
                        u.setEmail(email);
                        u.setTelephone(telephone);
                        if (address != "") {
                            u.setAddress(address);
                        }
                        if (postcode != "") {
                            u.setPostcode(Integer.parseInt(postcode));
                        }
                        UserService userService = new UserService();
                        try {
                            userService.register(u);
                            out.write(name + "注册成功");
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } else {
                        out.write(name + "已存在，请重新输入用户名\r\n");
                        out.write("3秒后跳转注册页面");
                        response.setHeader("Refresh", "3;url=Register.jsp");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        else{
            out.write("验证码不相等,3秒后跳转注册页面重新注册");
            response.setHeader("Refresh", "3;url=Register.jsp");
        }
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
    public static boolean queryName(String name) throws SQLException {//查找该用户名是否存在
        ResultSet rs= JDBCHelper.query("select * from user where name=?",name);

        if(rs==null||!rs.next()){
            System.out.println(rs);
            return true;

        }
        System.out.println(rs);
        return false;
    }

}